$(function() {

	// 获取当前页面的URL
	var fullUrl = window.location.href;

	// 提取查询字符串
	var queryString = fullUrl.split("?")[1];

	// 解析查询字符串
	var params = {};
	if (queryString) {
		var pairs = queryString.split('&');
		for (var i = 0; i < pairs.length; i++) {
			var pair = pairs[i].split('=');
			var key = decodeURIComponent(pair[0]);
			var value = decodeURIComponent(pair[1] || '');
			params[key] = value;
		}
	}

	// 访问参数
	var checkStr = params.check_time;
	var leaveStr = params.leave_time;
	var city = params.city;
	var peopleNum = params.peopleNum || 1;

	$(".city").val(city);
	$(".check").val(checkStr);
	$(".leave").val(leaveStr);
	$("#peopleInput").val(peopleNum + "人");
	$("#btn" + peopleNum).addClass("btn-selected");

	if (checkStr === undefined & leaveStr === undefined & city === undefined) {
		$.ajax({
			url: "http://localhost:8080/hotel/findAll",
			async: false,
			method: "get",
			success: function(data) {
				query(data);
			}
		});
	} else {
		$.ajax({
			url: "http://localhost:8080/hotel/like4book",
			async: false,
			method: "get",
			data: {
				people_num: peopleNum,
				city_location: city,
				check_time: checkStr,
				leave_time: leaveStr
			},
			success: function(data) {
				query(data);
			}
		});
	}

	$(".type li").click(function() {
		var num = $(this).val();
		num++;
		$("#btn" + num).addClass("btn-selected");
		$("#btn" + num).siblings().removeClass("btn-selected");
	})

	$(".room-type button").click(function() {
		var id = $(this).attr("id")
		var regex = /(\d+)/;
		var match = id.match(regex);

		var number
		if (match && match[1]) {
			number = match[1];
		}
		number--
		$('.peopleInput').val($('li[value=' + number + ']').text())

	})



	$(".like").click(function() {
		var city = $(".city").val();
		var check = $(".check").val();
		var leave = $(".leave").val();
		var num = $(".people-num").val();

		var regex = /(\d+)/; // 匹配一个或多个数字
		var match = num.match(regex);

		var number
		if (match && match[1]) {
			number = match[1];
		}

		$.ajax({
			url: "http://localhost:8080/hotel/like4book",
			async: false,
			method: "get",
			data: {
				people_num: number,
				city_location: city,
				check_time: check,
				leave_time: leave
			},
			success: function(data) {
				query(data);
			}
		});
	});

	// 获取到idea传来的数据并渲染到页面上
	function query(data) {
		$("#find-all").empty();
		if (data.t === null) {
			$("#hotel-sum").html(0);
			return;
		}
		var hotels = data.t;

		let num = 5;

		let str = getData(hotels, 0, 5);
		$("#find-all").html(str);

		$("#hotel-sum").html(hotels.length);

		function getData(hotels, start, end) {
			let str = "";
			if (hotels != null) {
				let zdata = hotels.slice(start, end);
				for (let i = 0; i < zdata.length; i++) {

					var sr = zdata[i].hotelImg;
					var srs = sr.split(";")


					var roomtype_count = null;
					var min_price = null;
					var assess_count = null;

					$.ajax({
						url: "http://localhost:8080/hotel/findHotelById",
						async: false,
						method: "get",
						data: {
							hotelId: zdata[i].id
						},
						success: function(data) {
							roomtype_count = data.t.roomtype_count;
							min_price = data.t.min_price;
							assess_count = data.t.assess_count;
						}
					});


					let score = zdata[i].hotelScore;

					var evaluate = '';

					if (score >= 4.9) {
						evaluate = '很棒';
					} else if (score === 4.8) {
						evaluate = '棒';
					} else if (score === 4.7) {
						evaluate = '挺好';
					} else if (score === 4.6) {
						evaluate = '好';
					} else if (score === 4.5) {
						evaluate = '还不错';
					} else {
						evaluate = '说实话兄弟，这个就真一般吧';
					}

					str += `<li>
						<div class="row clearfix detail-list">
							<div class="col-md-4 column">
								<div class="hotel-img">
									<a href="#">
										<img alt="140x140" style="width: 330px;height: 240px;" src="` + srs[0] + `" class="img-rounded" />
									</a>
								</div>
							</div>
							<div class="col-md-4 column">
								<div class="hotel_title">
									<a href="#">
										<h4>
											` + zdata[i].hotelName + `
										</h4>
									</a>
								</div>
								<div class="hotel-info">
									<ul class="nums">
										<li class="rating">
											<em>` + zdata[i].hotelScore + `</em>
											分
											<br>
											<strong>` + evaluate + `</strong>
										</li>
										<li>
											<a href="#">
												<em>` + assess_count + `</em>
												<br>
												住客评价
											</a>
										</li>
										<li class="split"></li>
						
										<li>
											<a href="#">
												<em>` + roomtype_count + `</em>
												种
												<br>
												房间类型
											</a>
										</li>
									</ul>
									<p class="summary"></p>
								</div>
								<div class="location">
									<span>
										<i class="iocon-location"></i>
										位于:
										<a href="detail.html">` + zdata[i].hotelAddress + `</a>
									</span>
								</div>
							</div>
							<div class="col-md-4 column">
								<div class="hotel_datail">
									<p>
										<a href="detail.html"><span>` + min_price + `</span>元起 »</a>
										<br>
										<a class="hotelInfo" id=` + zdata[i].id + `>酒店详情 »</a>
									</p>
								</div>
						
							</div>
						</div>
					</li>`
				}
			}
			return str;
		}

		$(window).scroll(function() {
			var scrollTop = $(this).scrollTop();
			var scrollHeight = $(document).height();
			var windowHeight = $(this).height();

			if (scrollTop + windowHeight + 300 >= scrollHeight) {
				if (num <= hotels.length) {
					let sum = getData(hotels, num, num + 5);
					num = num + 5;
					$("#find-all").append(sum);
				}
			}
		})
	}

	$(".hotelInfo").click(function() {
		var hotelId = $(this).attr("id");
		var check_time = $("#checkinDate").val();
		var leave_time = $("#checkoutDate").val();

		window.location.href = 'detail.html?hotelId=' + hotelId + '&check_time=' + check_time +
			'&leave_time=' + leave_time;
	})

	// $(".like-address").click(function() {

	// 	var address = $("#like-address").val();

	// 	$.ajax({
	// 		url: "http://localhost:8080/hotel/likeHotelByAddress",
	// 		method: "get",
	// 		data: {
	// 			name: address
	// 		},
	// 		success: function(data) {
	// 			$("#find-all").empty();

	// 			var hotels = data.t;

	// 			let num = 5;

	// 			let str = getData(hotels, 0, 5);
	// 			$("#find-all").html(str);

	// 			$("#hotel-sum").html(hotels.length);

	// 			function getData(hotels, start, end) {
	// 				let str = "";
	// 				let zdata = hotels.slice(start, end);
	// 				for (let i = 0; i < zdata.length; i++) {

	// 					let score = zdata[i].hotelScore;

	// 					var evaluate = '';

	// 					if (score >= 4.9) {
	// 						evaluate = '很棒';
	// 					} else if (score === 4.8) {
	// 						evaluate = '棒';
	// 					} else if (score === 4.7) {
	// 						evaluate = '挺好';
	// 					} else if (score === 4.6) {
	// 						evaluate = '好';
	// 					} else if (score === 4.5) {
	// 						evaluate = '还不错';
	// 					} else {
	// 						evaluate = '说实话兄弟，这个就真一般吧';
	// 					}

	// 					str += `<li>
	// 						<div class="row clearfix detail-list">
	// 							<div class="col-md-4 column">
	// 								<div class="hotel-img">
	// 									<a href="#">
	// 										<img alt="140x140" src="./static/image/hotel/hotel1.jpeg" class="img-rounded" />
	// 									</a>
	// 								</div>
	// 							</div>
	// 							<div class="col-md-4 column">
	// 								<div class="hotel_title">
	// 									<a href="#">
	// 										<h4>
	// 											` + zdata[i].hotelName + `
	// 										</h4>
	// 									</a>
	// 								</div>
	// 								<div class="hotel-info">
	// 									<ul class="nums">
	// 										<li class="rating">
	// 											<em>` + zdata[i].hotelScore + `</em>
	// 											分
	// 											<br>
	// 											<strong>` + evaluate + `</strong>
	// 										</li>
	// 										<li>
	// 											<a href="#">
	// 												<em>390条</em>
	// 												<br>
	// 												住客评价
	// 											</a>
	// 										</li>
	// 										<li class="split"></li>

	// 										<li>
	// 											<a href="#">
	// 												<em>5</em>
	// 												种
	// 												<br>
	// 												房间类型
	// 											</a>
	// 										</li>
	// 									</ul>
	// 									<p class="summary"></p>
	// 								</div>
	// 								<div class="location">
	// 									<span>
	// 										<i class="iocon-location"></i>
	// 										位于:
	// 										<a href="detail.html">` + zdata[i].hotelAddress + `</a>
	// 									</span>
	// 								</div>
	// 							</div>
	// 							<div class="col-md-4 column">
	// 								<div class="hotel_datail">
	// 									<p>
	// 										<a href="detail.html"><span>3390</span>元起 »</a>
	// 										<br>
	// 										<a href="detail.html">酒店详情 »</a>
	// 									</p>
	// 								</div>

	// 							</div>
	// 						</div>
	// 					</li>`
	// 				}
	// 				return str;
	// 			}

	// 			$(window).scroll(function() {
	// 				var scrollTop = $(this).scrollTop();
	// 				var scrollHeight = $(document).height();
	// 				var windowHeight = $(this).height();

	// 				if (scrollTop + windowHeight + 300 >= scrollHeight) {
	// 					if (num <= hotels.length) {
	// 						let sum = getData(hotels, num, num + 5);
	// 						num = num + 5;
	// 						$("#find-all").append(sum);
	// 					}
	// 				}
	// 			})

	// 		}
	// 	});
	// });
})